Can show frequency counts (2D histogram) or value of a third variable
Can be used for continuous or categorical data
rows: genes
columns: samples
color: change in gene expression level
Source: https://warwick.ac.uk/fac/sci/moac/people/students/peter_cock/r/heatmap/
ggplot2geom_tile with numerical data, compare to geom_point
x <- 1:3
y <- c(5, 2, 7)
df <- data.frame(x, y)
g1 <- ggplot(df, aes(x, y)) + geom_point()
g2 <- ggplot(df, aes(x, y)) + geom_tile()
grid.arrange(g1, g2, nrow = 1)df$w <- c(1.4, .4, .2)
df$h <- c(.5, 1.3, .8)
ggplot(df, aes(x, y)) + geom_tile(aes(width = w, height = h))xmin <- 1:3
xmax <- 2:4
ymin <- c(5, 2, 7)
ymax <- c(6, 3, 8)
df <- data.frame(xmin, xmax, ymin, ymax)
ggplot(df, aes(xmin = xmin, xmax = xmax, ymin = ymin,
ymax = ymax)) + geom_rect()x <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
y <- c(1, 2, 3, 1, 2, 3, 1, 2, 3)
df <- data.frame(x, y)
ggplot(df, aes(x, y)) + geom_raster()df$z <- c("A", "B", "C", "B", "A", "A", "B", "C", "B")
ggplot(df, aes(x, y)) + geom_raster(aes(fill = z))theme_heat <- theme_classic() +
theme(axis.line = element_blank(),
axis.ticks = element_blank())
ggplot(df, aes(x, y)) + geom_raster(aes(fill = z)) +
theme_heat(doesn’t work with geom_raster())
grade <- rep(c("first", "second", "third"), 3)
subject <- rep(c("math", "reading", "gym"), each = 3)
statescore <- sample(50, 9) + 50
df <- data.frame(grade, subject, statescore)
ggplot(df, aes(grade, subject, fill = statescore)) +
geom_tile(color = "white") +
coord_equal() + theme_heatgrade <- rep(c("first", "second", "third", "fourth"), 3)
subject <- rep(c("math", "reading", "gym"), each = 4)
statescore <- sample(50, 12) + 50
df <- data.frame(grade, subject, statescore)
ggplot(df, aes(grade, subject, fill = statescore)) +
geom_tile(color = "white") +
coord_equal() + theme_heatdf$grade <- forcats::fct_relevel(df$grade, "fourth", after = Inf)
ggplot(df, aes(grade, subject, fill = statescore)) +
geom_tile(color = "white") +
coord_equal() + theme_heatlibrary(vcdExtra)
library(dplyr)
orderedclasses <- c("Farm", "LoM", "UpM", "LoNM", "UpNM")
mydata <- Yamaguchi87
mydata$Son <- factor(mydata$Son, levels = orderedclasses)
mydata$Father <- factor(mydata$Father,
levels = orderedclasses)
japan <- mydata %>% filter(Country == "Japan")
uk <- mydata %>% filter(Country == "UK")
us <- mydata %>% filter(Country == "US")Japan
ggplot(japan, aes(x = Father, y = Son)) +
geom_tile(aes(fill = Freq), color = "white") +
coord_fixed() + theme_heatUK
ggplot(uk, aes(x = Father, y = Son)) +
geom_tile(aes(fill = Freq), color = "white") +
coord_fixed() + theme_heatUS
ggplot(us, aes(x = Father, y = Son)) +
geom_tile(aes(fill = Freq), color = "white") +
coord_fixed() + theme_heatggplot(mydata, aes(x = Father, y = Son)) +
geom_tile(aes(fill = Freq), color = "white") +
coord_fixed() + facet_wrap(~Country) + theme_heatmydata2 <- mydata %>% group_by(Country) %>%
mutate(Total = sum(Freq)) %>% ungroup()
ggplot(mydata2, aes(x = Father, y = Son)) +
geom_tile(aes(fill = Freq/Total), color = "white") +
coord_fixed() + facet_wrap(~Country) + theme_heat